package com.archstoday.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.archstoday.R;
import com.archstoday.adapter.ArticleListAdapter;
import com.archstoday.common.AsRes;
import com.archstoday.common.SpfUtils;
import com.archstoday.entity.ArticleListEntity;
import com.archstoday.entity.UserInfoEntity;
import com.archstoday.utils.OAuthUtils;
import com.archstoday.utils.ToolUtils;
import com.archstoday.view.PullToRefreshLayout;
import com.base.http.common.BaseHttpClient;
import com.base.utils.ImageUtil;
import com.base.utils.MySharedPreferencesMgr;
import com.base.utils.StringUtil;
import com.xw.repo.refresh.PullListView;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 用户文章列表页面
 */
public class ArticleListActivity extends AsScrollBaseActivity implements PullToRefreshLayout.OnRefreshListener {

    private CircleImageView mHeadIcon;
    private TextView mNameTv, mNumTv;
    private PullToRefreshLayout mPullLayout;
    private PullListView mPullList;

    private ArticleListAdapter mAdapter;
    private int page;
    private boolean isMe;
    private String articleUserId;

    @Override
    protected void initView(Bundle bundle) {
        setContentView(R.layout.activity_article_list);
        mHeadIcon = (CircleImageView) findViewById(R.id.user_head_icon);
        mNameTv = (TextView) findViewById(R.id.user_name_tv);
        mNumTv = (TextView) findViewById(R.id.article_num_tv);
        mPullLayout = (PullToRefreshLayout) findViewById(R.id.pull_layout);
        mPullList = (PullListView) findViewById(R.id.pull_list);
        mPullLayout.setOnRefreshListener(this);
    }

    @Override
    protected void initData(Bundle bundle) {
        mAdapter = new ArticleListAdapter(mContext);
        mPullList.setAdapter(mAdapter);
        isMe = getIntent().getBooleanExtra("isMe", false);

        if (isMe) {
            UserInfoEntity.DataBean dataBean = OAuthUtils.getInstance().getUserInfo();
            if (dataBean != null) {
                ImageUtil.setCropBitmap(mContext, mHeadIcon,
                        dataBean.getUserImg(), R.mipmap.user_header_icon);
                mNameTv.setText(dataBean.getName());
            }
            articleUserId = MySharedPreferencesMgr.getString(SpfUtils.USER_ID, "");
        } else {
            articleUserId = getIntent().getStringExtra("user_id");
        }

        page = 1;
        upData();
    }

    @Override
    protected void initListener() {
        setOnClickListener(R.id.back_btn);

        mPullList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(mContext, NewDetailsActivity.class);
                intent.putExtra("id", Integer.valueOf(mAdapter.getItem(i).getId()));
                intent.putExtra("isNews", true);
                startActivity(intent);
            }
        });
    }

    @Override
    public void treatClickEvent(int id) {
        switch (id) {
            case R.id.back_btn:
                appFinish();
                break;
        }
    }

    private void upData() {
        initParameter();
        mParams.put("userId", MySharedPreferencesMgr.getString(SpfUtils.USER_ID, ""));
        mParams.put("token", MySharedPreferencesMgr.getString(SpfUtils.USER_TOKEN, ""));
        mParams.put("articleUserId", articleUserId);
        mParams.put("page", page);
        mParams.put("pageSize", 10);

        sendEncryptPostRequest(AsRes.getInstance().getUrl(AsRes.GET_USER_ARTICLE),
                AsRes.GET_USER_ARTICLE, mParams, ArticleListEntity.class);
    }

    @Override
    protected void onSuccess(String content, BaseHttpClient client, Object parse) {
        mPullLayout.refreshFinish(true);
        mPullLayout.loadMoreFinish(true);
        if (client.getUrlIdentifier() == AsRes.GET_USER_ARTICLE) {
            ArticleListEntity entity = (ArticleListEntity) parse;
            if (ToolUtils.isSuccess(entity.getStatus())) {
                if (!isMe) {
                    ImageUtil.setCropBitmap(mContext, mHeadIcon,
                            entity.getData().getArticleUserImg(), R.mipmap.user_header_icon);
                    mNameTv.setText(entity.getData().getArticleUserName());
                }

                mNumTv.setText(StringUtil.replace(getString(R.string.article_list_num),
                        "*", String.valueOf(entity.getData().getArticleSize())));

                if (entity.getData().getArticleList().size() > 0) {
                    if (page == 1) {
                        mAdapter.setData(entity.getData().getArticleList());
                    } else {
                        mAdapter.addData(entity.getData().getArticleList());
                    }
                    page++;
                }
            } else if (entity.getStatus() == 10005) {
                MySharedPreferencesMgr.setString(SpfUtils.USER_ID, "");
                MySharedPreferencesMgr.setString(SpfUtils.USER_TOKEN, "");
                OAuthUtils.getInstance().cleanUserInfo();

                showToast("登录信息已过期，请重新登录");
                Intent intent = new Intent(mContext, LoginActivity.class);
                startActivity(intent);
            } else {
                showToast(entity.getMsg());
            }
        }
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        page = 1;
        upData();
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        upData();
    }
}